<?php
// +----------------------------------------------------------------------
// |     【PurchaseMain】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class PurchaseMain extends Model{
    use SoftDelete;
     protected $deleteTime = "delete_time";
     
     protected static $prefix;
      
     public function __construct(array $data = []){
         parent::__construct($data);
         self::$prefix = env('database.prefix'); // 获取表名前缀
     }
     
    // 获取列表
    public static function getList($where = []){
        $limit = I('get.limit/d');
        
        // 日期相关
        if ($year = I("year")) {
            $where[] = ["main.year", "=", $year];
        }
        
        if ($month = I("month")) {
            $where[] = ["main.month", "=", $month];
        }
        
        if ($day = I("day")) {
            $where[] = ["main.day", "=", $day];
        }
        
        // 物流相关（归属采购表 info）
        if ($exp_method = I("exp_method")) {
            $where[] = ["info.exp_method", "=", $exp_method];
        }
        
        if ($exp_name = I("exp_name")) {
            $where[] = ["info.exp_name", "=", $exp_name];
        }
        
        // 面料相关（归属采购表 info）
        if ($fabric_code = I("fabric_code")) {
            $where[] = ["info.fabric_code", "like", "%" . $fabric_code . "%"];
        }
        
        if ($color_code = I("color_code")) {
            $where[] = ["info.color_code", "=", $color_code];
        }
        
        // 供应商相关（归属采购表 info）
        if ($sup_code = I("sup_code")) {
            $where[] = ["info.sup_code", "like", "%" . $sup_code . "%"];
        }
        
        if ($sup_color = I("sup_color")) {
            $where[] = ["info.sup_color", "=", $sup_color];
        }
        
        if ($sup_name = I("sup_name")) {
            $where[] = ["info.sup_name", "like", "%" . $sup_name . "%"];
        }
        
        // 采购类型
        if ($pur_kind = I("pur_kind")) {
            $where[] = ["info.pur_kind", "=", $pur_kind];
        }
        
        // 销售名称（归属员工表 staff）
        if ($staff_name = I("staff_name")) {
            $where[] = ["staff.name", "like", "%" . $staff_name . "%"];
        }
        
        // 订单状态（明确支持 status=0）
        $pur_status = I("pur_status"); 
        if ($pur_status !== '') {
            $where[] = ["main.status", "=", (int)$pur_status];
        }
        
        // 订单状态（明确支持 status=0）
        if ($nodeal = I("nodeal")) {
            $where[] = ["main.status", "<>", 99];
        }
        
        
        $list = self::alias('main')
        // 关联员工表
        ->leftJoin(self::$prefix . 'staff_main staff', 'main.staff_id = staff.id')
         // 关联采购表
         ->leftJoin(self::$prefix . 'purchase_info info', 'main.id = info.pur_id ')
         // 关联采购表
         ->leftJoin(self::$prefix . 'fabric_main f', 'f.id = info.fabric_id ')
         // 按条件查询
         ->where($where)
         // 指定字段
         ->field("main.id,main.status,main.pur_sn,main.create_time,main.sketch,main.delivery,main.pick_info,main.pur_type,staff.name as staff_name,staff.short_name,staff.store_name,info.id as info_id,info.pur_id,info.fabric_code,info.fabric_name,info.color_code,info.color_hex,info.color_name,info.gary,info.count,info.unit,info.sup_name,info.sup_code,info.sup_color,info.exp_method,info.exp_name,info.kg_info,info.remark,info.pur_kind,info.cut_payment,info.proof,f.sup_empty")
         // 排序
         ->order([
             'main.status' => 'asc',
             'main.create_time' => 'desc',
         ])
         ->append(['create_time2'])
         // 分页
         ->paginate($limit);

        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	// 获取列表数据
	public static function listData($where=[],$order="",$n_page=0,$limit=20){
		$data=self::order($order)->where($where);
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();
		
		return $data;
	}
	
	// 获取某个单条记录
	public static function info($id){
		if(!is_array($id)){
			$model=self::find($id);
		}else{
			$model=self::where($id)->find();
		}
		return $model;		
	}
    
    // 获取打印数据
    public static function labelPrintData($T,$info,$print=[],$name='purchaseLabel'){
    	
    	$data=[
    	    "method" =>"printreport",
    	    "ReportType" =>"gridreport",
    	    "ReportName" => $name.'.grf',
    	    "ReportVersion" =>"1",
    	    "ReportUrl" => get_host()."/static/tool/print/".$name.".grf",
    		"PrinterName" => $print['printer_name2'],
    	    "Preview" => $print['preview'],
    	    "token" => md5($T['pur_sn']),
    	    "taskid" => $T['pur_sn'],
    		"Control" => [],
    		"Field" => [],
    		"Parameter"=>[],
    		"Data" => $info,
    	];

    	$control=[
    		'pur_sn' 		=> $T['pur_sn'],
    		'store_name'    => $T['staff']['store_name'],
            'staff_name'    => $T['staff']['name'],
    		'create_time' 	=> $T['create_time'],
    	];
        
    	$parameter=[
            
    	];
    	
    	// Control循环替换内容
    	foreach($control as $k=>$c){
    		$data['Control'][]=[
    			'type' =>'AsStaticBox',
    			'name' => $k,
    			'value' => $c,
    			'required' =>false
    		];
    	};
    	
    	// Parameter循环替换内容
    	foreach($parameter as $k=>$c){
    		$data['Parameter'][]=[
    			'type' =>'',
    			'name' => $k,
    			'value' => $c,
    			'required' =>false
    		];
    	};
    	
    	//	获取所有字段名
    	$field=array_keys($info[0]);
        
    	//循环替换内容
    	foreach($field as $f){
    		$data['Field'][]=[
    			'type' =>'ftString',
    			'name' => $f,
    			'size' => 255,
    			'required' =>false
    		];
    	};
    	
    	// 循环产品
    	// for($i=0;$i<count($data['Data']);$i++){
    	// 	$data['Data'][$i]['fabric_code']=$data['Data'][$i]['fabric_code'];
    	// }
    	return $data;
    }
	
	/** 获取器 **/
	public function getCreateTime2Attr($value,$data){
		$value=uc_time_ago($data['create_time']);
	    return $value;
	}
	
	public function getOcreateTimeAttr($value,$data){
		$value=$data['create_time'];
	    return $value;
	}
}
